package com.ocean.everyday.d003_strongpasswordcheckerii;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.cn/problems/strong-password-checker-ii/
 */
public class StrongPasswordCheckerii2 {
    public static void main(String[] args) {
        StrongPasswordCheckerii2 checker = new StrongPasswordCheckerii2();
        System.out.println(checker.strongPasswordCheckerII("IloveLe3tcode!"));
        System.out.println(checker.strongPasswordCheckerII("Me+You--IsMyDream"));
        System.out.println(checker.strongPasswordCheckerII("1aB!"));
    }

    /**
     * 检查是否是强度密码
     *
     * @param password 密码串
     * @return
     */
    public boolean strongPasswordCheckerII(String password) {
        if (password.length() < 8) {
            return false;
        }
        Set<Character> set = new HashSet<Character>() {
            {
                add('!');
                add('@');
                add('#');
                add('$');
                add('%');
                add('^');
                add('&');
                add('*');
                add('(');
                add(')');
                add('-');
                add('+');
            }
        };
        boolean hasSpecial = false;
        boolean hasDigit = false;
        boolean hasLower = false;
        boolean hasUpper = false;
        for (int i = 0; i < password.length(); i++) {
            char cur = password.charAt(i);
            if (i > 0 && password.charAt(i) == password.charAt(i - 1)) {
                return false;
            }
            if (Character.isDigit(cur)) {
                hasDigit = true;
            }
            if (Character.isLowerCase(cur)) {
                hasLower = true;
            }
            if (set.contains(cur)) {
                hasSpecial = true;
            }
            if (Character.isUpperCase(cur)) {
                hasUpper = true;
            }
        }
        return hasSpecial && hasDigit && hasLower && hasUpper;
    }
}
